package com.exam.system.service.impl;

import com.exam.system.dao.ExamQuestionDao;
import com.exam.system.entity.ExamQuestion;
import com.exam.system.redis.RedisUtils;
import com.exam.system.service.ExamQuestionService;
import com.exam.system.service.ExamTagsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExamQuestionServiceImpl implements ExamQuestionService {
   @Autowired
   private ExamQuestionDao examQuestionDao;
   @Autowired
   private ExamTagsService examTagsService;
    @Autowired
    private RedisUtils redisUtils;
    //获取所有题目列表 分页显示
    @Override
    @Transactional
    public PageInfo<ExamQuestion> findAll(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ExamQuestion> all = examQuestionDao.findAll();
        if(all!=null&all.size()>0){
            for (ExamQuestion examQuestion : all) {
                String[] tagList = new String[0];
                String tag = examQuestion.getTags();
                if(tag!=null&& tag.length()!=0) {
                    tagList = tag.split(",");
                }
                examQuestion.setTagList(tagList);
            }
        }
        PageInfo<ExamQuestion> pageInfo=new PageInfo(all);
        return pageInfo;
    }
    //禁用题目
    @Override
    @Transactional
    public int forbidden(Integer id) {
        return examQuestionDao.forbidden(id);
    }
    //启用题目
    @Override
    @Transactional
    public int enable(Integer id) {
        return examQuestionDao.enable(id);
    }
    //查找所有
    @Override
    @Transactional
    public List<ExamQuestion> findAll() {
        return examQuestionDao.findAll();
    }
    //多条件分页查询
    @Override
    @Transactional
    public PageInfo<ExamQuestion> findQuestionsByQuery(Integer page, Integer pageSize, String question, String tags, String type) {
        PageHelper.startPage(page,pageSize);
        List<ExamQuestion> questionsByQuery = examQuestionDao.findQuestionsByQuery(question, tags, type);
        if(questionsByQuery!=null&questionsByQuery.size()>0){
            for (ExamQuestion examQuestion : questionsByQuery) {
                String[] tagList = new String[0];
                String tag = examQuestion.getTags();
                if(tag!=null&& tag.length()!=0){
                    tagList=tag.split(",");
                }
                examQuestion.setTagList(tagList);
            }
        }
        PageInfo<ExamQuestion> pageInfo=new PageInfo<>(questionsByQuery);
        return pageInfo;
    }

    /**
     * 根据题目类型和题库查询题目
     * @param type 题目类型
     * @param quesBankName 题库名称
     * @return 返回题目集合
     */
    @Override
    public PageInfo<ExamQuestion> findQuestionByTypeAndLibrary(Integer page, Integer pageSize, String type, String quesBankName) {
        PageHelper.startPage(page, pageSize);
        List<ExamQuestion> questionList = examQuestionDao.findQuestionByTypeAndLibrary(type, quesBankName);
        return new PageInfo<>(questionList);
    }

    /**
     * 根据题目类型和题库查询题目和答案
     * @param page 当前页码
     * @param pageSize 当前页数量
     * @param type 题目类型
     * @param quesBankName 题库名字
     * @return 返回题目和答案
     */
    @Override
    public PageInfo<ExamQuestion> findQuestionAndAnswerByTypeAndLibrary(Integer page, Integer pageSize, String type, String quesBankName) {
        PageHelper.startPage(page, pageSize);
        List<ExamQuestion> questionList = examQuestionDao.findQuestionAndAnswerByTypeAndLibrary(type, quesBankName);

        return new PageInfo<>(questionList);
    }

    //批量禁用题目
    @Override
    public int forbiddenBat(List<ExamQuestion> list) {
        List<Integer> ids=new ArrayList();
        for (ExamQuestion examQuestion : list) {
            ids.add(examQuestion.getId());
        }
        return examQuestionDao.forbiddenBat(ids);
    }

    //批量启用题目
    @Override
    public int enableBat(List<ExamQuestion> list) {
        List<Integer> ids=new ArrayList();
        for (ExamQuestion examQuestion : list) {
            ids.add(examQuestion.getId());
        }
        return examQuestionDao.enableBat(ids);
    }
    /**
     * 查询所有题目
     * @param id
     * @return
     */
    @Override
    public List<ExamQuestion> findQuestionByPaperId(Integer id) {
        return examQuestionDao.findQuestionByPaperId(id);
    }


    //删除试题
    @Override
    public int delQuestion(Integer id) {
        return examQuestionDao.delQuestion(id);
    }


    @Override
    public List<Object> findAllQuestionAndTags(Integer page, Integer pageSize) {
        List list = new ArrayList<Object>();
        list.add(findAll(page, pageSize));
        return list;
    }

    @Override
    public List<Object> findQuestionsByQueryAndTags(Integer page, Integer pageSize, String question, String  tags, String type) {
        List list = new ArrayList<Object>();
        list.add(findQuestionsByQuery(page, pageSize, question, tags, type));
        return list;
    }

    @Override
    public ExamQuestion findByQuesId(Integer id) {
        ExamQuestion question = examQuestionDao.findByQuesId(id);
        String tags = question.getTags();
        String[] tagList = new String[0];
        if(tags!=null&& tags.length()!=0) {
            tagList = tags.split(",");
        }
        question.setTagList(tagList);
        return question;
    }
}
